package com.alkaid.anime.service.impl;

import com.alkaid.anime.mapper.ThumbMapper;
import com.alkaid.anime.service.ThumbService;
import com.alkaid.anime.utils.RedisUtils;
import com.alkaid.commons.core.CacheKeyDefinition;
import com.alkaid.commons.entity.Thumb;
import com.alkaid.commons.ex.UnknownErrorException;
import com.alkaid.commons.utils.SnowflakeIdWorker;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Kylin
 */
@Service
public class ThumbServiceImpl implements ThumbService {

    private final ThumbMapper thumbMapper;

    private final RedisUtils redisUtils;

    public ThumbServiceImpl(ThumbMapper thumbMapper, RedisUtils redisUtils) {
        this.thumbMapper = thumbMapper;
        this.redisUtils = redisUtils;
    }

    @Override
    public Boolean getThumb(Thumb select) throws UnknownErrorException {
        // 获取key
        String key = CacheKeyDefinition.getThumbKey(select.getUserId(), select.getAnimeId());
        // 从缓存中查询
        Boolean result = (Boolean) redisUtils.getFromRedis(key);
        // 如果缓存中没有则从数据库查询
        if (result == null) {
            Thumb thumb = getThumbBySelect(select);
            if (thumb == null) {
                return false;
            }
            result = thumb.getIsDeleted() == 0;
            redisUtils.save2Redis(key, result, CacheKeyDefinition.CACHE_TIME_DAY);
        }
        return result;
    }

    @Override
    public void setThumb(Thumb select) throws UnknownErrorException {
        Thumb thumb = getThumbBySelect(select);
        String key = CacheKeyDefinition.getThumbKey(select.getUserId(), select.getAnimeId());
        if (thumb == null) {
            SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 2);
            select.setId(idWorker.nextId());
            select.setIsDeleted(0);
            Integer result = thumbMapper.insert(select);
            if (result < 1) {
                throw new UnknownErrorException("点赞失败");
            }
            redisUtils.save2Redis(key, true, CacheKeyDefinition.CACHE_TIME_DAY);
            return;
        }
        int isDeleted = thumb.getIsDeleted() == 0 ? 1 : 0;
        thumb.setIsDeleted(isDeleted);
        thumb.setUserId(null);
        thumb.setAnimeId(null);
        Integer result = thumbMapper.updateById(thumb);
        if (result < 1) {
            throw new UnknownErrorException(isDeleted == 0 ? "点赞失败" : "取消点赞失败");
        }
        redisUtils.save2Redis(key, isDeleted == 0, CacheKeyDefinition.CACHE_TIME_DAY);
    }

    private Thumb getThumbBySelect(Thumb select) {
        HashMap<SFunction<Thumb, ?>, Object> map = new HashMap<>();
        map.put(Thumb::getUserId, select.getUserId());
        map.put(Thumb::getAnimeId, select.getAnimeId());
        return thumbMapper.selectOne(new LambdaQueryWrapper<Thumb>().allEq(map));
    }
}
